package com.zyq.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyq.exception.CustomException;
import com.zyq.model.common.ResponseBean;
import com.zyq.model.entity.Article;
import com.zyq.model.entity.Food;
import com.zyq.model.request.ContentDto;
import com.zyq.service.IFoodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: zyq
 * @create: 2022-04-08
 **/
@Api( tags = "饮食")
@RestController
@RequestMapping("/food")
public class FoodController {


    @Autowired
    private IFoodService bloodSugarService;

    @ApiOperation("查询")
    @GetMapping("/list")
    public ResponseBean list(ContentDto food) {
        PageHelper.startPage(food.getPageNum(), food.getPageSize());
        Food tempFood = new Food().setIntroduce(food.getContent());
        List<Food> list = bloodSugarService.selectFoodList(tempFood);
        PageInfo<Food> selectPage = new PageInfo<>(list);
        if (list == null || list.size() < 0) {
            throw new CustomException("查询失败(Query Failure)");
        }
        Map<String, Object> result = new LinkedHashMap<>(16);
        result.put("total", selectPage.getTotal());
        result.put("pages", selectPage.getPages());
        result.put("data", selectPage.getList());
        result.put("data",list);
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);
    }


    @ApiOperation("新增")
    @PostMapping
    public ResponseBean add(@RequestBody Food bloodSugar) {
        bloodSugarService.insertFood(bloodSugar);
        return new ResponseBean(HttpStatus.OK.value(), "操作成功", null);
    }

    @ApiOperation("修改")
    @PutMapping
    public ResponseBean edit(@RequestBody Food bloodSugar) {
        bloodSugarService.updateFood(bloodSugar);
        return new ResponseBean(HttpStatus.OK.value(), "操作成功", null);
    }


    @ApiOperation("删除")
    @DeleteMapping("/{ids}")
    public ResponseBean remove(@PathVariable Long[] ids) {
        bloodSugarService.deleteFoodByIds(ids);
        return new ResponseBean(HttpStatus.OK.value(), "操作成功", null);
    }
}
